_flavor=postmarketos-mainline
_config="config-${_flavor}.${CARCH}"

pkgname=linux-${_flavor}

pkgver=4.14_rc7
_pkgver=${pkgver/_/-}
_kernver=${pkgver%_rc*}
_mainver=${_kernver%.*}
_patchlevel=${_kernver/$_mainver./}
_basever=${_mainver}.$((_patchlevel-1))
pkgrel=2

arch="all"
pkgdesc="Linux for pmOS supported chipsets (mainline, more bleeding-edge than stable)"
url="https://kernel.org/"
depends="postmarketos-mkinitfs"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev libressl-dev"
options="!strip !check !tracedeps"
install=
source="
	https://cdn.kernel.org/pub/linux/kernel/v$_mainver.x/linux-$_basever.tar.xz
	linux-v$_basever-v$_kernver-rc1.patch::https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/rawdiff/?id=v$_kernver-rc1&id2=v$_basever
	linux-v$_kernver-rc1-v$_pkgver.patch::https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/rawdiff/?id=v$_pkgver&id2=v$_kernver-rc1
	config-${_flavor}.armhf
	config-${_flavor}.aarch64
	config-${_flavor}.x86_64
	001-sony-amami-dts.patch
"

subpackages="$pkgname-dev::$CBUILD_ARCH"

license="GPL2"
_abi_release=$_pkgver
_carch=${CARCH}
case "$_carch" in
aarch64*) _carch="arm64" ;;
arm*) _carch="arm" ;;
ppc*) _carch="powerpc" ;;
s390*) _carch="s390" ;;
esac

HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

ksrcdir="$srcdir/linux-$_basever"

prepare() {
	local _patch_failed=
	cd "$ksrcdir"
	# first apply patches in specified order
	for i in $source; do
		case $i in
		*.patch|*.patch::*)
			_patch=${i%::*}
			msg "Applying $_patch..."
			if ! patch -s -p1 -N -i "$srcdir"/$_patch; then
				echo $_patch >>failed
				_patch_failed=1
			fi
			;;
		esac
	done

	if ! [ -z "$_patch_failed" ]; then
		error "The following patches failed:"
		cat failed
		return 1
	fi

	mkdir -p "$srcdir"/build
	cp -v "$srcdir"/$_config "$srcdir"/build/.config
	make -C "$ksrcdir" O="$srcdir"/build ARCH="$_carch" HOSTCC="$HOSTCC" \
		olddefconfig
}



# this is so we can do: 'abuild menuconfig' to reconfigure kernel
menuconfig() {
	cd "$srcdir"/build
	make ARCH="$_carch" menuconfig
	cp .config "$startdir"/$_config
}

build() {
	cd "$srcdir"/build
	unset LDFLAGS
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-${_flavor}" \
		CFLAGS_MODULE=-fno-pic
}

package() {
	cd "$srcdir/build/arch/${_carch}/boot"

	if [ "$CARCH" == "aarch64" ]; then
		install -Dm644 "$srcdir/build/arch/${_carch}/boot/Image" \
			"$pkgdir/boot/vmlinuz-$_flavor"
	else
		install -Dm644 "$srcdir/build/arch/${_carch}/boot/"*zImage \
			"$pkgdir/boot/vmlinuz-$_flavor"
	fi

	install -D "$srcdir/build/include/config/kernel.release" \
		"$pkgdir/usr/share/kernel/$_flavor/kernel.release"

	cd "$srcdir"/build

	local _install
	case "$CARCH" in
	aarch64*|arm*)	_install="modules_install dtbs_install" ;;
	*)		_install="modules_install" ;;
	esac

	make -j1 $_install \
		ARCH="$_carch" \
		INSTALL_MOD_PATH="$pkgdir" \
		INSTALL_DTBS_PATH="$pkgdir/usr/share/dtb"
}


dev() {
	# copy the only the parts that we really need for build 3rd party
	# kernel modules and install those as /usr/src/linux-headers,
	# simlar to what ubuntu does
	#
	# this way you dont need to install the 300-400 kernel sources to
	# build a tiny kernel module
	#
	pkgdesc="Headers and script for third party modules for postmarketos kernel"
	depends="gmp-dev bash perl"
	local dir="$subpkgdir"/usr/src/linux-headers-${_abi_release}

	# first we import config, run prepare to set up for building
	# external modules, and create the scripts
	mkdir -p "$dir"
	cp "$srcdir"/$_config "$dir"/.config
	make -j1 -C "$ksrcdir" O="$dir" ARCH="$_carch" HOSTCC="$HOSTCC" \
		olddefconfig prepare modules_prepare scripts

	# needed for 3rd party modules
	# https://bugzilla.kernel.org/show_bug.cgi?id=11143
	case "$CARCH" in
	ppc*) (cd "$dir" && make arch/powerpc/lib/crtsavres.o);;
	esac

	# remove the stuff that points to real sources. we want 3rd party
	# modules to believe this is the soruces
	rm "$dir"/Makefile "$dir"/source

	# copy the needed stuff from real sources
	#
	# this is taken from ubuntu kernel build script
	# http://kernel.ubuntu.com/git/ubuntu/ubuntu-zesty.git/tree/debian/rules.d/3-binary-indep.mk

	cd "$ksrcdir"
	find . -path './include/*' -prune \
		-o -path './scripts/*' -prune -o -type f \
		\( -name 'Makefile*' -o -name 'Kconfig*' -o -name 'Kbuild*' -o \
		   -name '*.sh' -o -name '*.pl' -o -name '*.lds' \) \
		-print | cpio -pdm "$dir"
	cp -a scripts include "$dir"
	find $(find arch -name include -type d -print) -type f \
		| cpio -pdm "$dir"

	install -Dm644 "$srcdir"/build/Module.symvers \
		"$dir"/Module.symvers

	mkdir -p "$subpkgdir"/lib/modules/${_abi_release}
	ln -sf /usr/src/linux-headers-${_abi_release} \
		"$subpkgdir"/lib/modules/${_abi_release}/build
}
sha512sums="a557c2f0303ae618910b7106ff63d9978afddf470f03cb72aa748213e099a0ecd5f3119aea6cbd7b61df30ca6ef3ec57044d524b7babbaabddf8b08b8bafa7d2  linux-4.13.tar.xz
ec42158f59f105995a3ccde025085343b5e6ba60de778c14cacf9aa34c40ae2e23058a03a1e6545f174c694620bbb0e423947f784c43e1474adfdcb7f866b3b6  linux-v4.13-v4.14-rc1.patch
f120fc30e759c5c1dfd391c13bfdfa05675fbef02afdad3dce0fd22f02455f4d2ed545c1d4bddf2ada26827f5d54adc6813e2f927df3ebe18cd78ad1c99e0098  linux-v4.14-rc1-v4.14-rc7.patch
a75225750193cf31c8b6c15e7748699f842ecadc9ed573f0b10f42bb5343b4e092bcb65b359d170a42a102a4ddfe09d63c1d67550f4b99c29295fa22e1d7b995  config-postmarketos-mainline.armhf
6310035a0a4c028cc98cab92919cca54be9b79ee9fe63211e7a05876d7befdbd8506a2f80548e228e014e99b0f91f63946352789d8d2f2134c66914163f66ab3  config-postmarketos-mainline.aarch64
3d42c60061d12a436d19ffdf00623ea71ca31f16f690ae82ebf872d35177c283b4038cd5e270f4e610d6cf6861cef73ac63c995f1f4cfe9d6f2a4a8648ba28fb  config-postmarketos-mainline.x86_64
e34315167868c2539b2cb548b4305a5e16527b8d5b39843945bf3b0c2a67d70f7d9c93443bac2d28a744aa85012c241c68c5a4f09f0ad3e35ba8945bdaed2e2d  001-sony-amami-dts.patch"
